<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Session
 * 
 * @author kubin
 */


include_once 'Model/model.php';
include_once 'cookie.php';

class Session 
{

    //put your code here 
    private $cookie;
    private $model;

    public function __construct($model) {
        $this->model = $model;
        $this->cookie = new Cookie();
        //TODO: rewrite create session
        if (!isset($_SESSION)) {
            session_start();
            $this->setSession2($this->cookie);
          //    $this->writeSession("Contructor");
        }
    }

    public function sessionDefault() {
        $this->setSession(false, 0, '', 0, $this->cookie, false);
    }

    public function setSession($logged, $uid, $username, $status, $cookie, $remember) {
        $_SESSION['logged'] = $logged;
        $_SESSION['uid'] = $uid;
        $_SESSION['username'] = $username;
        $_SESSION['status'] = $status;
        $_SESSION['cookie'] = $cookie;
        $_SESSION['remember'] = $remember;
    }

    public function setSession1($user, $remember) {
        $this->setSession(true, $user->uid, $user->username, $user->status, $this->cookie, TRUE);
    }

    public function setSession2($cookie) {
        $this->setSession($cookie->isLogged(), $cookie->getUid(), $cookie->getUsername(), $cookie->getStatus(), $cookie, true);
    }

    public function doLogin($username, $password) {
     //   echo $username . ", ". $password . '<br/>';
        $user = $this->model->checkingLoging($username, $password);
        
        if ($user != null) {
//            echo "not null ".$user->uid. $user->username. $user->status;
            $this->setSession1($user, true);
            $this->cookie->setupCookie($user->uid, $user->username, $user->status, true);
        }else{
        //    echo "    null";
        }
   //     $this->writeSession("doLogin");
    }

    public function doLogout() {
        $this->sessionDefault();
        session_write_close();
        $this->cookie->close();
      //  $this->writeSession("doLogout");
    }

    public function isLogged() {
        if (isset($_SESSION['logged']))
            return $_SESSION['logged'];
        else
            return FALSE;
    }

    public function checkUserPermision($status) {
        if (!isset($_SESSION)) {
            return false;
        }
        if ($status <= $_SESSION['status']) {
            return true;
        } else {
            return false;
        }
    }

    public function getUserStatus() {
        if (isset($_SESSION['status'])) {
            return $_SESSION['status'];
        }
        else
            return USER_NOTYET_LOGIN;
    }
    
    public function write($block, $string) {
        $testFile = fopen("testCookie.txt", 'a');
        fwrite($testFile, "Session.php\n");
        fwrite($testFile, date("h:m:s" . "\n"));
        fwrite($testFile, $block . ":\n");
        fwrite($testFile, "> " . $string . "\n\n");
        fclose($testFile);
    }
    
    public function writeSession($block) {
        $testFile = fopen("testCookie.txt", 'a');
        fwrite($testFile, "Session.php\n");
        fwrite($testFile, date("h:m:s" . "\n"));
        fwrite($testFile, $block . " - session:\n");
        if (isset($_SESSION["uid"], $_SESSION["username"], $_SESSION["status"], $_SESSION["logged"])) {
            fwrite($testFile, "> uid: \t" . $_SESSION["uid"] . "\n");
            fwrite($testFile, "> username: \t" . $_SESSION["username"] . "\n");
            fwrite($testFile, "> status: \t" . $_SESSION["status"] . "\n");
            fwrite($testFile, "> logged: \t" . $_SESSION["logged"] . "\n");
        }
        else{
           fwrite($testFile, "> not yet setup cookie\n"); 
        }
        fwrite($testFile, "\n");
        
        fclose($testFile);
    }

}

?>
